package com.anzia.study_12.slice;

import com.anzia.study_12.ResourceTable;
import com.anzia.study_12.utils.DeviceUtils;
import com.anzia.study_12.utils.UIUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityOperation;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.utils.net.Uri;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {

	private Button localAddBtn, localReadBtn, localDelBtn, localUpdateBtn;
	private Uri uri;
	DataAbilityHelper helper;

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_main);

		localAddBtn = (Button) findComponentById(ResourceTable.Id_fa_da_data_local_add_btn);
		localAddBtn.setClickedListener(mClickedListener);

		localReadBtn = (Button) findComponentById(ResourceTable.Id_fa_da_data_local_read_btn);
		localReadBtn.setClickedListener(mClickedListener);

		localDelBtn = (Button) findComponentById(ResourceTable.Id_fa_da_data_local_delete_btn);
		localDelBtn.setClickedListener(mClickedListener);

		localUpdateBtn = (Button) findComponentById(ResourceTable.Id_fa_da_data_local_update_btn);
		localUpdateBtn.setClickedListener(mClickedListener);

		String uriStr = "dataability://" + DeviceUtils.getDeviceId() + "com.anzai.study_12.SimpleDataAbility";
		uri = Uri.parse(uriStr);

	}

	private Component.ClickedListener mClickedListener = new Component.ClickedListener() {
		@Override
		public void onClick(Component component) {
			int componentId = component.getId();
			helper = DataAbilityHelper.creator(MainAbilitySlice.this);
			switch (componentId) {
				case ResourceTable.Id_fa_da_data_local_add_btn: {
					// 写新增数据的代码
					try {
						// ValuesBucket values = new ValuesBucket();
						// values.putString("firstName", "A");
						// values.putString("lastName", "nzia");
						// values.putInteger("age", 18);
						// values.putDouble("balance", 1000.5);
						// int insertRowId = helper.insert(uri, values);
						// ValuesBucket[] valuesBuckets = new ValuesBucket[10];
						// for (int i = 0; i < 10; i++) {
						// 	valuesBuckets[i] = new ValuesBucket();
						// 	valuesBuckets[i].putString("firstName", "li" + String.valueOf(100 + i));
						// 	valuesBuckets[i].putString("lastName", "si" + String.valueOf(100 + i))
						// 	valuesBuckets[i].putInteger("age", 28 + i);
						// 	valuesBuckets[i].putDouble("balance", 3000.5 + i);
						// }
						//
						// int i = helper.batchInsert(uri, valuesBuckets);
						//
						// UIUtils.showTip(MainAbilitySlice.this, "====RowId: " + i);

						ArrayList<DataAbilityOperation> ops = new ArrayList<>();

						ValuesBucket values = new ValuesBucket();
						values.putString("firstName", "A");
						values.putString("lastName", "nzia");
						values.putInteger("age", 18);
						values.putDouble("balance", 1000.5);
						DataAbilityOperation op1 = DataAbilityOperation.newInsertBuilder(uri).withValuesBucket(values).build();

						ValuesBucket value = new ValuesBucket();
						value.putString("firstName", "A");
						value.putString("lastName", "nzia");
						value.putInteger("age", 18);
						value.putDouble("balance", 1000.5);
						DataAbilityOperation op2 = DataAbilityOperation.newInsertBuilder(uri).withValuesBucket(value).build();

						ops.add(op1);
						ops.add(op2);

						helper.executeBatch(uri, ops);

					} catch (Exception e) {
						e.printStackTrace();
					}
				} break;
				case ResourceTable.Id_fa_da_data_local_read_btn: {
					// 读本地数据
					try {
						DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();
						dataAbilityPredicates.orderByDesc("userId");
						ResultSet resultSet = helper.query(uri, new String[]{"userId", "firstName", "lastName", "age", "balance"}, dataAbilityPredicates);
						UIUtils.showTip(MainAbilitySlice.this, "已读取数据");
						if (resultSet.getRowCount() > 0) {
							resultSet.goToFirstRow();
							for (int i = 0; i < resultSet.getRowCount(); i++) {
								System.out.println("查询出来的数据" + resultSet.getInt(resultSet.getColumnIndexForName("userId")) + ", " +
										resultSet.getString(resultSet.getColumnIndexForName("firstName")) + ", " +
										resultSet.getString(resultSet.getColumnIndexForName("lastName")) + ", " +
										resultSet.getInt(resultSet.getColumnIndexForName("age")) + ", " +
										resultSet.getDouble(resultSet.getColumnIndexForName("balance")));
								resultSet.goToNextRow();
							}
						}
					} catch (DataAbilityRemoteException e) {
						e.printStackTrace();
					}
				} break;
				case ResourceTable.Id_fa_da_data_local_delete_btn: {
					// 删除本地数据
					try {
						DataAbilityPredicates pd = new DataAbilityPredicates();
						pd.equalTo("userId", 1);
						helper.delete(uri, pd);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} break;
				case ResourceTable.Id_fa_da_data_local_update_btn: {
					// 更新本地数据
					try {
						DataAbilityPredicates pd = new DataAbilityPredicates();
						pd.equalTo("userId", 1);
						ValuesBucket valuesBucket = new ValuesBucket();
						valuesBucket.putString("firstName", "An");
						valuesBucket.putString("lastName", "zhi");
						valuesBucket.putInteger("age", 28);
						valuesBucket.putDouble("balance", 40000.5);
						int update = helper.update(uri, valuesBucket, pd);
						UIUtils.showTip(MainAbilitySlice.this, "" + update);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} break;
				default:
					break;
			}
		}
	};

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}
}
